<?php
/**
 * 
 * CREATE  TABLE IF NOT EXISTS `bayes_map` (
  `bayes_map_id` INT NOT NULL AUTO_INCREMENT ,
  `bayes_feature_id` INT NULL ,
  `bayes_category_id` INT NULL ,
  `feature_category_count` INT NULL ,
  PRIMARY KEY (`bayes_map_id`) ,
  UNIQUE INDEX `pair` (`bayes_feature_id` ASC, `bayes_category_id` ASC) )
ENGINE = InnoDB
 * 
 */ 
class Bayes_map_data_Model extends BayesDataTemplate
{
	
	// Table primary key and value
	protected $primary_key				= 'bayes_map_id';

	
	function find_feature_category( Bayes_feature_data_Model $feature, Bayes_categoty_data_model $category )
	{
		return $this->set_text_key(array('bayes_feature_id' => $feature->bayes_feature_id, 'bayes_category_id'=> $category->bayes_category_id ));
	}
	
	
	function increment_features_category_counters( Bayes_categoty_data_model $category, BayesFeatureSet $feature_set )
	{
		
		foreach($feature_set  as $feature)
		{
			
			
			$this->find_feature_category($feature, $category);
			if(!$this->exists())
			{
				
				$this->create();
			}
			$this->feature_category_count  = $this->feature_category_count ? $this->feature_category_count + 1 : 1;
			
			$this->save();
			
		}
	}
	/**
	 * This is only used when learning 
	 * Get the total number of features that have been matched to a given category
	 * For category-feature 1 to 1 maches get $this->feature_category_count
	 * @param Bayes_categoty_data_model $category
	 * @return type 
	 */
	function get_feature_count_for_category(Bayes_categoty_data_model $category)
	{
		return $this->where(array('bayes_category_id'=> $category->bayes_category_id ))->count_all();
	}
}